<?php
//验证码 水印2  缩略
//<?php


//验证码
function verifyImage($type=1,$length=4,$pixel=15,$line=3,$sess_name="verify"){

$width=110;
$height=30;
$image=imagecreatetruecolor($width,$height);
$white=imagecolorallocate($image,255,255,255);
$black=imagecolorallocate($image,0,0,0);

imagefilledrectangle($image,1,1,$width-2,$height-2,$white);

$chars=buildRandomString($type,$length);

$_SESSION[$sess_name]=$chars;
$fontfiles=Array("1.ttf","2.ttf","3.ttf");

$fontColor[]=imagecolorallocate($image,0x15,0x15,0x15);
$fontColor[]=imagecolorallocate($image,0x95,0x1e,0x04);
$fontColor[]=imagecolorallocate($image,0x93,0x14,0xa9);
$fontColor[]=imagecolorallocate($image,0x12,0x81,0x0a);
$fontColor[]=imagecolorallocate($image,0x06,0x3a,0xd5);

for($i=0;$i<$length;$i++){
    $size=mt_rand(18,26);
    $angle=mt_rand(-15,15);
    $x=5+$i*$size;
    $y=mt_rand(20,26);
    $fontfile="./fonts/" . $fontfiles [mt_rand(0,count($fontfiles)-1)];
    $color=imagecolorallocate($image,mt_rand(0,50),mt_rand(60,100),mt_rand(100,210));
    $text=substr($chars,$i,1);
    $color=$fontColor[mt_rand(0,4)];
    imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
}
if($pixel){
    for($i=0;$i<$pixel;$i++){
        imagesetpixel ($image,mt_rand(0,$width-1),mt_rand(0,$height-1),$black);
    }
}

if($line){

    for($i=1;$i<$line;$i++){
        $color=imagecolorallocate($image,mt_rand(0,70),mt_rand(0,150),mt_rand(160,200));
        imageline($image,mt_rand(0,$width-1),mt_rand(0,$height-1),mt_rand(0,$width-1),mt_rand(0,$height-1),$color);
    }
}
  header("content-type:image/gif");
  ob_clean();//清除前面所有文本(显示验证码)；
   imagegif($image);
   imagedestroy($image);
}





function thumb($filename,$destination=null,$dst_w=null,$dst_h=null,$isReservedSource=true,$scale=0.5){
//缩略图 $filename,路径
    list($src_w,$src_h,$imagetype)=getimagesize($filename);
    if(is_null($dst_w)||is_null($dst_h)){
    $dst_w=ceil($src_w*$scale);
    $dst_h=ceil($src_h*$scale);
    }
    $mime=image_type_to_mime_type($imagetype);
    $createFun=str_replace("/","createfrom",$mime);// image/png
    $outFun=str_replace("/",null,$mime);
    $src_image=$createFun($filename);
    $dst_image=imagecreatetruecolor($dst_w,$dst_h);
    imagecopyresampled($dst_image,$src_image,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);
    if($destination&&!file_exists(dirname($destination))){
    mkdir(dirname($destination),0777,true);
    }
    $dstFilename=$destination==null?getUniName().".".getExt($filename):$destination;
    $outFun($dst_image,$dstFilename);
    imagedestroy($src_image);
    imagedestroy($dst_image);
    if(!$isReservedSource){
    unlink($filename);
    }
    return $dstFilename;
}


function waterText($filename,$text="你好",$fontfile="simhei.ttf"){
    $fileInfo=getimagesize($filename);
    $mime=$fileInfo['mime'];
    $createFun=str_replace("/","createfrom",$mime);
    $outFun=str_replace("/",null,$mime);
    $image=$createFun($filename);
   $color=imagecolorallocatealpha($image,255,0,0,50);
   $fontfile="../fonts/{$fontfile}";
   imagettftext($image,14,0,0,14,$color,$fontfile,$text);
   $outFun($image,$filename);
   //类型
   //header('content-type:image/png');
   //imagepng($image);
   imagedestroy($image);

}//文字

function waterPic($dstFile,$srcFile="../admin/images/webLogo.jpg",$pct=100){
    $srcFileInfo=getimagesize($srcFile);
    $src_w=$srcFileInfo[0];
    $src_h=$srcFileInfo[1];
    $dstFileInfo=getimagesize($dstFile);
    $srcMime=$srcFileInfo['mime'];
    $dstMime=$dstFileInfo['mime'];
    $createSrcFun=str_replace("/","createfrom",$srcMime);
    $createDstFun=str_replace("/","createfrom",$dstMime);
    $outDstFun=str_replace("/",null,$dstMime);
    $dst_im=$createDstFun($dstFile);
    $src_im=$createSrcFun($srcFile);
    imagecopymerge($dst_im,$src_im,0,0,0,0,$src_w,$src_h,$pct);
    $outDstFun($dst_im,$dstFile);
    //header('content-type:image/png');
    // imagepng($src_im);覆盖
    //imagepng($dst_im);
    imagedestroy($src_im);
    imagedestroy($dst_im);
}//图片


